const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let str;
rl.on("line", (line)=>{
    if(str === undefined) {
        str = line.trim()
        console.log(getResult(str))
        // clear
        str = undefined
    }
})

/**
 *  求严格递增最小修改次数
 * @param str {string} 只包含A或B的字符串
 * @return {number}
 */
function getResult(str) {
    let countA = str.replaceAll("B", "").length;
    let length = str.length;

    if(countA ===0 || countA === length) return 0; // 不用改
    let result;
    result = Math.min(countA, length - countA) // 全A或全B

    // 前A&后B
    let currentA = 0
    for (let i = 0; i < length ; i++) {
        if(str[i] === "A") currentA++;
        result = Math.min(result, i + 1 + countA - 2*currentA)
    }
    return result;
}